#include "spliter.h"
#include <string>

void SimpleSpliter::open(std::string filename) {
	closeFile();
	_filename = filename;
	openFile();
}

void SimpleSpliter::close(){
	closeFile();
}

bool SimpleSpliter::hasNextWord() {
	input >> word;
	return !input.eof();
}

std::string SimpleSpliter::nextWord() {
	return word;
}

bool CLangSpliter::hasNextWord() {
	word = "";

	input.get(prev_char);
//	input >> prev_char;

	while(!input.eof()) {
		switch(state) {
			case START:
				if(!std::isspace(prev_char)) {
					if(std::isalpha(prev_char)) {
						word += prev_char;
						state = VAR;
					} else if(std::isdigit(prev_char)) {
						word += prev_char;
						state = DIGIT;
					}
				}
				break;
			case VAR:
				if(std::isalnum(prev_char)) {
					word += prev_char;
				} else {
					state = START;
					return true;
				}
				break;
			case DIGIT:
				if(std::isdigit(prev_char)) {
					word += prev_char;
				} else {
					state = START;
					return true;
				}
				break;
		}
		input.get(prev_char);
	//	input >> prev_char;
	}

	return false;
}

